package vivek.project.pocketmoney.common.controls.ComboDataGridColumn
{
	import flash.events.Event;
	import mx.containers.VBox;
	import mx.controls.AdvancedDataGrid;
	import mx.controls.ComboBox;
	import mx.controls.Label;
	import mx.controls.listClasses.ListData;
	import mx.events.AdvancedDataGridEvent;
	import vivek.project.pocketmoney.admin.model.AdminConstants;

	public class ComboBoxHeaderRenderer extends VBox
	{

		public function ComboBoxHeaderRenderer()
		{
			trace("Renderer Start: " + new Date().toTimeString());
			super();
			width=combo.width;
			unscaledWidth=super.unscaledWidth;
			percentWidth=100;
			addChild(labels);
			//combo.rowCount=15;
			combo.percentWidth=100;
			addChild(combo);
			horizontalScrollPolicy="off"
			verticalScrollPolicy="off"
			combo.addEventListener("change", changeHandler);
			trace("Renderer End: " + new Date().toTimeString());
			//dropdownFactory = new ClassFactory(SeparatorList);
		}
		private var _data:ComboBoxHeaderColumn;
		private var combo:ComboBox=new ComboBox();
		private var labels:Label=new Label();

		override public function get data():Object
		{
			return _data;
		}

		override public function set data(value:Object):void
		{
			super.data=value;
			_data=value as ComboBoxHeaderColumn;
			combo.data=_data;
			combo.labelFunction=_data.comboLabelFunction;
			combo.listData=value as ListData;
			//_data.width=combo.width;
			//combo.labelField=_data.comboLabelField;
			labels.text=_data.headerText;
			//	combo.itemRenderer=_data.comboBoxItemRenderer;
			AdvancedDataGrid(combo.addEventListener(AdvancedDataGridEvent.HEADER_RELEASE, sortEventHandler));
			combo.dataProvider=_data.comboBoxDataProvider;
			combo.selectedIndex=_data.selectedIndex;
		}

		override protected function measure():void
		{
			super.measure();
			measuredWidth=combo.width;
		}

		private function changeHandler(event:Event):void
		{
			data.selectedIndex=combo.selectedIndex;
			data.selectedItem=combo.selectedItem;
			if (combo.selectedItem != CommonConstants.ALL_STRING)
			{
				combo.setStyle("color", "#0000FF");
				setStyle("iconColor", "#0000FF");
			}
			else
			{
				combo.setStyle("color", "#000000")
				setStyle("iconColor", "#000000");
			}
			data.dispatchEvent(event);
		}

		private function sortEventHandler(event:AdvancedDataGridEvent):void
		{
			if (event.itemRenderer == this)
				event.preventDefault();
		}
	}
}